package so.shanku.essential.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.adapter.CommentsAdapter;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.Constant;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jaelyn on 2015/10/6 0006.
 * 文章评论
 */
public class ArticalCommentsActivity extends BaseUIActivity {
	//
	@ViewInject(id = R.id.ptr_list)
	private PullToRefreshListView ptr_list;
	@ViewInject(id = R.id.nodata_layout) //无数据
	private LinearLayout nodataLayout;
    @ViewInject(id=R.id.empty)
    private TextView emptyTv;
	private CommentsAdapter commentsAdapter;

	/**
	 * 评论输入框，和发送按钮
	 */
	@ViewInject(id = R.id.comment_edit)
	private EditText pro_et_comment;
	@ViewInject(id = R.id.comment_submit, click = "commit_comment_click")
	private TextView comment_submit;
	@ViewInject(id = R.id.layout_comment)
	private RelativeLayout layout_comment_submit;
	/**
	 * 评论后标识
	 */
	private int iscomment = 0;
	/**
	 * 回复某人的信息
	 */
	private String reId;
	/**
	 * 平乱
	 */
	private List<JsonMap<String, Object>> dataList=new ArrayList<JsonMap<String, Object>>();

	private String articalId;//发现id
	private String commentId;
	private int flag = 0;//0:发现详情进入

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.artical_comments_layout);
		pro_et_comment.setFocusableInTouchMode(true);
        emptyTv.setText(R.string.no_comments);
		initData(getIntent());
        getServerData(0);
	}


	private void initData(Intent intent) {
        commentsAdapter = new CommentsAdapter(this);
		articalId = intent.getStringExtra(ExtraKeys.Key_Msg1);
		setCenter_title(intent.getStringExtra(ExtraKeys.Key_Msg2));
        commentId = intent.getStringExtra(ExtraKeys.Key_Msg4);
		try {
			flag = intent.getIntExtra(ExtraKeys.Key_Msg3, 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
        if (flag == 0) {
        } else {
            setBtn_menuText(R.string.scan_article, onClickListener);
        }
        commentsAdapter.setFlag(flag);
		//getCommentsById(true); //test
		reId = "0";
        ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
        ptr_list.setOnRefreshListener(loadMoreListener);
        ptr_list.setOnItemClickListener(onItemClickListener);
	}


    AdapterView.OnItemClickListener onItemClickListener= new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position < 1) {
                return;
            }
            String userInfoId = dataList.get(position - 1).getStringNoNull("UserInfoId");

            if (userInfoId.equalsIgnoreCase(Utils.getUserId(mContext))) {
                toast.showToast(R.string.index_nocomment);
                pro_et_comment.setText("");
                pro_et_comment.setHint(R.string.comment_submit_hint);
                reId = "0";
            } else {
                String Rename = dataList.get(position - 1)
                        .getStringNoNull("Name");
                pro_et_comment.setHint("回复：" + Rename);
                reId = dataList.get(position - 1).getStringNoNull("Id");
                pro_et_comment.requestFocus();

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//隐藏软键盘
// imm.hideSoftInputFromWindow(editView.getWindowToken(), 0);
//显示软键盘
                imm.showSoftInput(pro_et_comment, InputMethodManager.SHOW_FORCED);
//切换软键盘的显示与隐藏
                //imm.toggleSoftInputFromWindow(pro_et_comment.getWindowToken(), 0, InputMethodManager.HIDE_NOT_ALWAYS);
//					}

                //}
            }
        }
    };

	/**
	 * 查看原文
	 */
	View.OnClickListener onClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
            if(!TextUtils.isEmpty(articalId)) {
                Intent intent = new Intent(mContext, ArticalDetailActivity.class);
                intent.putExtra(Constant.ID, articalId);
                jumpTo(intent, false);
            }
		}
	};

    /**
     * 刷新数据
     */
    private PullToRefreshBase.OnRefreshListener2 loadMoreListener = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase refreshView) {
            getServerData(1);
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase refreshView) {
            getServerData(2);
        }
    };

    /**
     * 获取服务器端数据
     * { "categoryId":0,"time": "","requestType":0}
     * @param requestType 请求类型 0:第一次请求,1刷新,2:加载下一页
     */
    public void getServerData(int requestType) {
        if(dataList.size()==0){
            requestType=0;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("articalId", articalId);
        params.put("requestType",requestType);
        if(requestType==0) {
            params.put("time", "");
        } else if (requestType == 1) {
            params.put("time",dataList.get(0).getStringNoNull("createTime"));
        }else {
            params.put("time",dataList.get(dataList.size()-1).getStringNoNull("createTime"));
        }

        GetDataQueue queue = new GetDataQueue();
        queue.setTag(requestType);

        if (flag == 0) {
            queue.setActionName(GetDataConfing.Action_GetDiscoverCommentsList);
            queue.setWhat(GetDataConfing.What_GetDiscoverCommentsList);
        } else {
            params.put("commentId", commentId);
            queue.setActionName(GetDataConfing.Action_GetArticalCommentsListMessage);
            queue.setWhat(GetDataConfing.What_GetArticalCommentsListMessage);
        }
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        getDataUtil.getData(queue);
    }


	/**
	 * 提交评论
	 *
	 * @param view
	 */
	public void commit_comment_click(View view) {
		if (TextUtils.isEmpty(pro_et_comment.getText())) {
			//toast.showToast(R.string.comment_content_isnull);
			return;
		}
		if (Utils.isHasLogin(mContext)) {
            getAddComment();
		} else {
			gotoLogin();
		}

	}

	/**
	 * 添加评论       {DiscoverId:1,Content:"xxxxxx",UserInfoId:9392}
	 *
	 * @param
	 * @return void 返回类型
	 * @throws
	 * @Title: getAddComment
	 * @Description: TODO作用：评论
	 */
	public void getAddComment() {
		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();

		params.put("articalId", articalId);
		params.put("userId", Utils.getUserId(mContext));
		params.put("content", pro_et_comment.getText());
		params.put("articalCommentId", reId);
		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_SendArticalComment);
		queue.setParamsNoJson(params);
		queue.setCallBack(callBack);
		queue.setWhat(GetDataConfing.What_SendArticalComment);
		getDataUtil.getData(queue);

	}




	private void validateData() {
//		if (dataList == null || dataList.isEmpty()) {
//			ptr_list.setVisibility(View.GONE);
//			nodataLayout.setVisibility(View.VISIBLE);
//			return;
//		} else {
//			ptr_list.setVisibility(View.VISIBLE);
//			nodataLayout.setVisibility(View.GONE);
		//}
	}

	private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

		@Override
		public void onLoaded(GetDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isCodeSuccess(mContext, entity.getInfo())) {
					if (entity.what == GetDataConfing.What_SendArticalComment) {
						pro_et_comment.setText("");
						pro_et_comment.setHint(R.string.comment_submit_hint);
						reId = "0";
						iscomment = 1;
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//隐藏软键盘
// imm.hideSoftInputFromWindow(editView.getWindowToken(), 0);
//显示软键盘
						imm.hideSoftInputFromWindow(pro_et_comment.getWindowToken(), 0);
						getServerData(1);
					} else if (entity.what == GetDataConfing.What_GetDiscoverCommentsList || entity.what == GetDataConfing.What_GetArticalCommentsListMessage) {
                        ArrayList<JsonMap<String, Object>> temp_data = (ArrayList<JsonMap<String, Object>>) JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info);
                        setAdapterData(temp_data, (Integer) entity.getTag());
					}
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
			ptr_list.onRefreshComplete();
		}

	};

    private void setAdapterData(ArrayList<JsonMap<String, Object>> temp_data, int requestType) {
        if (requestType == 0){
            if (temp_data.size() == 0) {
                ptr_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            } else {
                ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
                dataList.addAll(0, temp_data);
                commentsAdapter.setDatas(dataList);
                commentsAdapter.notifyDataSetChanged();
            }

        } else if (requestType == 1) {
            if (!temp_data.isEmpty()) {
                dataList.addAll(0, temp_data);
                commentsAdapter.setDatas(dataList);
                commentsAdapter.notifyDataSetChanged();
            }
        } else {
            if (temp_data.size() == 0) {
                ptr_list.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            } else {
                ptr_list.setMode(PullToRefreshBase.Mode.BOTH);
            }

            if (!temp_data.isEmpty()) {
                dataList.addAll(temp_data);
                commentsAdapter.setDatas(dataList);
                commentsAdapter.notifyDataSetChanged();
            }
        }

    }

}